package com.abbas.smartCity.ui.movie;

import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.abbas.smartCity.App;
import com.abbas.smartCity.Death;
import com.abbas.smartCity.R;
import com.abbas.smartCity.adapter.BannerImageAdapter2;
import com.abbas.smartCity.adapter.ListCommAdapter;
import com.abbas.smartCity.base.BaseActivity;
import com.abbas.smartCity.bean.movie.MovieBannerList;
import com.abbas.smartCity.bean.movie.MovieList;
import com.abbas.smartCity.retroft.ICallback;
import com.abbas.smartCity.view.MListView;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;

import java.util.List;
import java.util.stream.Collectors;

import retrofit2.Call;

public class MovieActivity extends BaseActivity {

    private Banner<String, BannerImageAdapter2<String>> banner;
    private EditText searchEt;
    private MListView lv;
    private Button loadmore;
    private ListCommAdapter<MovieList.RowsBean, ListCommAdapter.NewsHolder> listAdapter;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_movie;
    }

    @Override
    protected String setCustomTitle() {
        return "看电影";
    }

    @Override
    protected void initView() {

        banner = (Banner) findViewById(R.id.banner);
        searchEt = (EditText) findViewById(R.id.search_et);
        lv = (MListView) findViewById(R.id.lv);
        loadmore = (Button) findViewById(R.id.loadmore);
    }

    @Override
    protected void initData() {
        final List<MovieBannerList.RowsBean> movieBannerListRows = Death.movieBannerList.getRows();
//        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE)
//                .setImageLoader(App.urlLoader)
//                .setImages(movieBannerListRows.stream().map(MovieBannerList.RowsBean::getAdvImg).collect(Collectors.toList()))
//                .setBannerTitles(movieBannerListRows.stream().map(MovieBannerList.RowsBean::getAdvTitle).collect(Collectors.toList()))
//                .startAutoPlay();

        banner.setAdapter(new BannerImageAdapter2<String>(
                        movieBannerListRows.stream()
                                .map(MovieBannerList.RowsBean::getAdvImg)
                                .collect(Collectors.toList())))
                .setIndicator(new CircleIndicator(this))
                .start();

        getMovieList(null, 1, 5);
    }

    @Override
    protected void initEvent() {
        searchEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    getMovieList(searchEt.getText().toString(), null, null);
                    return true;
                }
                return false;
            }
        });

        loadmore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getMovieList(null, null, null);
                loadmore.setVisibility(View.GONE);
                showToast("所有电影已加载完毕");
            }
        });

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final MovieList.RowsBean item = listAdapter.getItem(position);
                final Intent intent = new Intent(MovieActivity.this, MovieDetailActivity.class);
                intent.putExtra("data", new Gson().toJson(item));
                startActivity(intent);
            }
        });
    }


    private void getMovieList(String name, Integer pageNum, Integer pageSize) {
        api.getMovieList(name, pageNum, pageSize).enqueue(new ICallback<MovieList>() {
            @Override
            public void onSuccess(Call<MovieList> call, MovieList data) {
                if (listAdapter == null) {
                    listAdapter = new ListCommAdapter<MovieList.RowsBean, ListCommAdapter.NewsHolder>(
                            data.getRows(),
                            ListCommAdapter.NewsHolder.class,
                            R.layout.news_item
                    ) {
                        @Override
                        protected void show(MovieList.RowsBean item, View convertView, NewsHolder viewHolder, int position, Context context) {
                            Glide.with(context)
                                    .load(App.getBaseUrl() + item.getCover())
                                    .into(viewHolder.im);

                            viewHolder.title.setText(item.getName());
                            viewHolder.sub.setText("简介：" + item.getIntroduction());
                            viewHolder.tag1.setText("上映时间:" + item.getPlayDate());
                            viewHolder.tag2.setText("时长：" + item.getDuration());
                        }
                    };
                    lv.setAdapter(listAdapter);
                } else {
                    listAdapter.resetData(data.getRows());
                }
            }
        });
    }
}